﻿//-----------------------------------------------------------------------
// <copyright file="ServiceCollectionExtensions.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: ServiceCollectionExtensions.cs
// * history : created by qinchaoyue 2017-10-24 12:19:38
// </copyright>
//-----------------------------------------------------------------------

using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Slwy.Mvc.Filters;

namespace Slwy.Mvc
{
    /// <summary>
    /// mvc 服务注册
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注册带有拦截的 mvc 服务
        /// </summary>
        /// <param name="services">服务集合</param>
        public static void AddMvcService(this IServiceCollection services)
        {
            // 添加跨域支持
            // services.AddCors();
            services
                .AddSingleton<Microsoft.AspNetCore.Http.IHttpContextAccessor,
                    Microsoft.AspNetCore.Http.HttpContextAccessor>();
            services.AddMvc(
                options =>
                    {
                        // 添加全局异常拦截
                        options.Filters.Add(new ErrorHandlingFilter());
                        options.Filters.Add(new ActioningHandingFilter());
                    }).AddJsonOptions(
                options =>
                    {
                        // 设置时间类型数据返回格式.
                        options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                        options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;

                        // options.SerializerSettings.ContractResolver = new DefaultContractResolver(); // 返回的json 大小写 跟随 model
                    });
        }
    }
}
